const BaseController = require("./base/base");
const Result = require("../utils/result");
const goodsModel = require("../model/goods");
const { getTokenUser, isAdmin } = require("../middlewares/auth");
class GoodsController extends BaseController {
  constructor() {
    super();
  }
  findGoodsList(req, res, next) {
    setTimeout(async ()=>{
      try {
        let { pagesize = 10, page = 1, goods_name = '', cat_one_id = '' } = req.query;
        if (!pagesize) pagesize = 10;
        if (!page) page = 1;
        let goods_state = 1; //已审核的
        let delete_time = 1 //已上架的
        let count = await goodsModel.getCount({ pagesize, page, goods_name, cat_one_id, goods_state, delete_time });
        let ret = await goodsModel.findGoodsList({ pagesize, page, goods_name, cat_one_id, goods_state, delete_time });
        res.json(Result.ok("success", { total: count[0].count, list: ret }));
      } catch (err) {
        res.json(Result.error(err));
      }
    })
  }
  getAll(req, res, next) {
    setTimeout(async ()=>{
      try {
        let { role: isadmin = "" } = getTokenUser(req);
        if (!isAdmin(isadmin)) return res.json(Result.fail("你没有管理员权限", []));
        let { pagesize = 10, page = 1, goods_name = '', cat_one_id = '' } = req.query;
        if (!pagesize) pagesize = 10;
        if (!page) page = 1;
        let count = await goodsModel.getCount({ pagesize, page, goods_name, cat_one_id });
        let ret = await goodsModel.findGoodsList({ pagesize, page, goods_name, cat_one_id });
        res.json(Result.ok("success", { total: count[0].count, list: ret }));
      } catch (err) {
        res.json(Result.error(err));
      }
    })
  }
  findById(req, res, next) {
    setTimeout(async ()=>{
      try {
        let { id } = req.params;
        let ret = await goodsModel.findById(id);
        res.json(Result.ok("success", ret));
      } catch (err) {
        res.json(Result.error(err));
      }
    })
  }
  findGoodsCategoryList(req, res, next) {
    setTimeout(async ()=>{
      try {
        let { pagesize = 10, page = 1, goods_name = '', cat_one_id = '' } = req.query;
        if (!pagesize) pagesize = 10;
        if (!page) page = 1;
        let count = await goodsModel.getCategoryCount({ pagesize, page, goods_name, cat_one_id });
        let ret = await goodsModel.findGoodsCategoryList({ pagesize, page, goods_name, cat_one_id });
        res.json(Result.ok("success", { total: count[0].count, list: ret }));
      } catch (err) {
        res.json(Result.error(err));
      }
    })
  }
  // insert(req, res, next) {
  // }
  update(req, res, next){
    setTimeout(async ()=>{
      try {
        let { role: isadmin = "" } = getTokenUser(req);
        if (!isAdmin(isadmin)) return res.json(Result.fail("你没有管理员权限", []));
        const { id } = req.params;
        let ret = await goodsModel.update({...req.body}, id);
        !ret.affectedRows ?
          res.json(Result.fail("修改失败", ret)) :
          res.json(Result.ok("修改成功", ret));
      } catch (err) {
        res.json(Result.error(err));
      }
    })
  }
  delById(req, res, next){
    setTimeout(async ()=>{
      try {
        let { role: isadmin = "" } = getTokenUser(req);
        if (!isAdmin(isadmin)) return res.json(Result.fail("你没有管理员权限", []));
        const { id } = req.params;
        let ret = await goodsModel.delById(id);
        !ret.affectedRows ?
          res.json(Result.fail("删除失败", ret)) :
          res.json(Result.ok("删除成功", ret));
      } catch (err) {
        res.json(Result.error(err));
      }
    })
  }
}
module.exports = new GoodsController().resolve();